package com.happy.cat.project.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.happy.cat.common.utils.StringUtils;
import com.happy.cat.common.utils.poi.ExcelUtil;
import com.happy.cat.framework.web.controller.BaseController;
import com.happy.cat.framework.web.domain.AjaxResult;
import com.happy.cat.framework.web.page.TableDataInfo;
import com.happy.cat.project.system.domain.SocialTrans;
import com.happy.cat.project.system.domain.UserBalancePetValueDetail;
import com.happy.cat.project.system.service.ISocialTransService;
import com.happy.cat.project.system.service.IUserBalancePetValueDetailService;
import com.happy.cat.framework.aspectj.lang.annotation.Log;
import com.happy.cat.framework.aspectj.lang.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 账号余额明细(猫粮)Controller
 *
 * @author verne
 * @date 2020-08-13
 */
@AllArgsConstructor
@RestController
@RequestMapping("/system/userBalancePetValueDetail" )
@Api("账号余额明细(猫粮) API")
public class UserBalancePetValueDetailController extends BaseController {

    private final IUserBalancePetValueDetailService iUserBalancePetValueDetailService;

    private final ISocialTransService transService;

    /**
     * 查询账号余额明细(猫粮)列表
     */
    @ApiOperation(value = "查询账号余额明细(猫粮)列表",notes = "查询账号余额明细(猫粮)列表",response = TableDataInfo.class)
    @PreAuthorize("@ss.hasPermi('system:userBalancePetValueDetail:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserBalancePetValueDetail userBalancePetValueDetail)
    {
        startPage();
        LambdaQueryWrapper<UserBalancePetValueDetail> lqw = new LambdaQueryWrapper<UserBalancePetValueDetail>();
        if (userBalancePetValueDetail.getType() != null){
            lqw.eq(UserBalancePetValueDetail::getType ,userBalancePetValueDetail.getType());
        }
        if (StringUtils.isNotBlank(userBalancePetValueDetail.getText())){
            lqw.eq(UserBalancePetValueDetail::getText ,userBalancePetValueDetail.getText());
        }
        if (userBalancePetValueDetail.getAmount() != null){
            lqw.eq(UserBalancePetValueDetail::getAmount ,userBalancePetValueDetail.getAmount());
        }
        if (userBalancePetValueDetail.getCurrentAmount() != null){
            lqw.eq(UserBalancePetValueDetail::getCurrentAmount ,userBalancePetValueDetail.getCurrentAmount());
        }
        if (userBalancePetValueDetail.getChangeTime() != null){
            lqw.eq(UserBalancePetValueDetail::getChangeTime ,userBalancePetValueDetail.getChangeTime());
        }
        if (userBalancePetValueDetail.getAccountId() != null){
            lqw.eq(UserBalancePetValueDetail::getAccountId ,userBalancePetValueDetail.getAccountId());
        }
        if (userBalancePetValueDetail.getAmountType() != null){
            lqw.eq(UserBalancePetValueDetail::getAmountType ,userBalancePetValueDetail.getAmountType());
        }
        List<UserBalancePetValueDetail> list = iUserBalancePetValueDetailService.list(lqw);

        Set<Long> transIds = list.stream().map(UserBalancePetValueDetail::getTransId).collect(Collectors.toSet());
        if(transIds.size()>0){
            Map<Long, SocialTrans> transMaps = transService.listByIds(transIds).stream().collect(Collectors.toMap(SocialTrans::getId, e -> e));
            list.forEach(e->{
                if(e.getTransId()!=null && e.getTransId() > 0){
                    e.setTrans(transMaps.get(e.getTransId()));
                }
            });
        }
        return getDataTable(list);
    }

    /**
     * 导出账号余额明细(猫粮)列表
     */
    @ApiOperation(value = "导出账号余额明细(猫粮)列表",notes = "导出账号余额明细(猫粮)列表",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:userBalancePetValueDetail:export')" )
    @Log(title = "账号余额明细(猫粮)" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(UserBalancePetValueDetail userBalancePetValueDetail) {
        LambdaQueryWrapper<UserBalancePetValueDetail> lqw = new LambdaQueryWrapper<UserBalancePetValueDetail>(userBalancePetValueDetail);
        List<UserBalancePetValueDetail> list = iUserBalancePetValueDetailService.list(lqw);
        ExcelUtil<UserBalancePetValueDetail> util = new ExcelUtil<UserBalancePetValueDetail>(UserBalancePetValueDetail. class);
        return util.exportExcel(list, "userBalancePetValueDetail" );
    }

    /**
     * 获取账号余额明细(猫粮)详细信息
     */
    @ApiOperation(value = "获取账号余额明细(猫粮)详细信息",notes = "获取账号余额明细(猫粮)详细信息",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:userBalancePetValueDetail:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iUserBalancePetValueDetailService.getById(id));
    }

    /**
     * 新增账号余额明细(猫粮)
     */
    @ApiOperation(value = "新增账号余额明细(猫粮)",notes = "新增账号余额明细(猫粮)",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:userBalancePetValueDetail:add')" )
    @Log(title = "账号余额明细(猫粮)" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserBalancePetValueDetail userBalancePetValueDetail) {
        return toAjax(iUserBalancePetValueDetailService.save(userBalancePetValueDetail) ? 1 : 0);
    }

    /**
     * 修改账号余额明细(猫粮)
     */
    @ApiOperation(value = "修改账号余额明细(猫粮)",notes = "修改账号余额明细(猫粮)",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:userBalancePetValueDetail:edit')" )
    @Log(title = "账号余额明细(猫粮)" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserBalancePetValueDetail userBalancePetValueDetail) {
        return toAjax(iUserBalancePetValueDetailService.updateById(userBalancePetValueDetail) ? 1 : 0);
    }

    /**
     * 删除账号余额明细(猫粮)
     */
    @ApiOperation(value = "删除账号余额明细(猫粮)",notes = "删除账号余额明细(猫粮)",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:userBalancePetValueDetail:remove')" )
    @Log(title = "账号余额明细(猫粮)" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iUserBalancePetValueDetailService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
